package cn.hnu.jpa.jpaExample.SuperManyToOneOneToMany;

import static org.junit.Assert.*;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.junit.Test;

import cn.hnu.jpa.jpaExample.ManyToOne.SingleWay.ManyAddress;
import cn.hnu.jpa.jpaExample.ManyToOne.SingleWay.ManyCustomer;

public class UnitTest {

	//@Test
	public void test() {
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa");
		EntityManager em = emf.createEntityManager();
		em.getTransaction().begin();
		SuperCustomer cpk = new SuperCustomer();
		SuperAddress addr1 = new SuperAddress();
		SuperAddress addr2 = new SuperAddress();
		cpk.setName("zdc");
		addr1.setAddr("123456");
		addr2.setAddr("789012");
		addr1.setCustomer(cpk);
		addr2.setCustomer(cpk);
		cpk.addSuperAddress(addr1);
		cpk.addSuperAddress(addr2);
		em.persist(cpk);
		em.getTransaction().commit();
		em.close();
		emf.close(); 
	}
//	@Test 测试是否可以逆向保存  在addr上加上cascade = all 就可以了
	public void test2() {
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa");
		EntityManager em = emf.createEntityManager();
		em.getTransaction().begin();
		SuperCustomer cpk = new SuperCustomer();
		SuperAddress addr1 = new SuperAddress();
		SuperAddress addr2 = new SuperAddress();
		cpk.setName("zdc");
		addr1.setAddr("123456");
		addr2.setAddr("789012");
		addr1.setCustomer(cpk);
		addr2.setCustomer(cpk);
		cpk.addSuperAddress(addr1);
		cpk.addSuperAddress(addr2);
		em.persist(addr1);
		em.getTransaction().commit();
		em.close();
		emf.close(); 
	}
}
